package IOStreamTest.FileStream;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
文件的复制，使用FileReader和FileWriter共同完成
 */
public class FileCopyTest2 {
    public static void main(String[] args) {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("src\\MapTest\\MapTest1.java");
            fw = new FileWriter("D:\\myFile.txt");

            //创建一个char数组，做临时存取数组容器
            char[] chars = new char[4];
            int readCount = 0;
            while((readCount = fr.read(chars)) != -1){
                //将数据写入磁盘文件中
                fw.write(chars);
            }

            fw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
